package com.qdsgjsfk.vision.ui;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.util.NetUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.TaskListResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by HP on 2019/9/12.
 */

public class ReceiveActivity extends BaseActivity {

    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_school)
    TextView tv_school;
    @BindView(R.id.tv_twice)
    TextView tv_twice;
    @BindView(R.id.tv_worker)
    TextView tv_worker;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_project)
    TextView tv_project;
    @BindView(R.id.btn_receive)
    TextView btn_receive;

    String taskId, userId, username;
    @BindView(R.id.ll_project)
    LinearLayout llProject;
    @BindView(R.id.iv_open_close)
    ImageView ivOpenClose;
    @BindView(R.id.ll_open_close)
    LinearLayout llOpenClose;

    @Override
    protected void initViews() {
        tv_name.setText( username + "邀请你加入下列任务" );
    }

    @OnClick(R.id.btn_receive)
    void btn_receive() {
        accept();
    }

    @Override
    protected void initData() {
        taskId = getIntent().getStringExtra( "taskId" );
        userId = getIntent().getStringExtra( "userId" );
        username = getIntent().getStringExtra( "username" );

        getTaskList();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_receive;
    }

    private void getTaskList() {
        RestProxy.getInstance().getTaskList(0, "", "", taskId, userId, "", "", new Observer<TaskListResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(TaskListResponse taskListResponse) {
                if (taskListResponse.data.list.size() > 0) {
                    tv_project.setText( taskListResponse.data.list.get( 0 ).checkOption );
                    tv_date.setText( taskListResponse.data.list.get( 0 ).taskTime );
                    tv_school.setText( taskListResponse.data.list.get( 0 ).taskOrgName );
                    tv_worker.setText( taskListResponse.data.list.get( 0 ).workers );
                    tv_twice.setText( taskListResponse.data.list.get( 0 ).planName );
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                NetUtil.onError( e, mContext );
            }

            @Override
            public void onComplete() {
            }
        } );
    }

    private void accept() {
        RestProxy.getInstance().accept( userId, taskId, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText( ReceiveActivity.this, "成功加入任务", Toast.LENGTH_SHORT ).show();
                finish();
            }

            @Override
            public void onError(Throwable e) {
                NetUtil.onError( e, mContext );
            }

            @Override
            public void onComplete() {

            }
        } );
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        // TODO: add setContentView(...) invocation
        ButterKnife.bind( this );
    }

    @OnClick(R.id.ll_open_close)
    public void onViewClicked() {
        if (llProject.getVisibility() == View.VISIBLE) {
            llProject.setVisibility( View.GONE );
            ivOpenClose.setImageDrawable( ContextCompat.getDrawable( ReceiveActivity.this, R.mipmap.icon_open ) );
        } else {
            llProject.setVisibility( View.VISIBLE );
            ivOpenClose.setImageDrawable( ContextCompat.getDrawable( ReceiveActivity.this, R.mipmap.icon_close_task ) );
        }
    }
}
